<template>
  <div class="header">
    <div class="background"></div>
    <div class="top">
      <div class="search" @click="goSearch">
        <div class="search-icon"></div>
        <div class="search-text">请输入门店名称/地址</div>
      </div>
      <div class="tocode"></div>
    </div>
    <div class="Internet">
      <!-- 网咖和电竞酒店标题 -->
      <div class="Internet-type">
        <div class="cafes" @click="isType" :class="istype == true ? 'active' : ''">网咖
          <div class="zindexmax1"></div>
        </div>
        <div class="hotel" @click="isType" :class="istype == false ? 'active' : ''">电竞酒店
          <div class="zindexmax2"></div>
        </div>
        <div @click="isType" :class="istype == false ? 'tranfrom3' : ''" class="tranfrom1"></div>
        <div @click="isType" :class="istype == true ? 'tranfrom4' : ''" class="tranfrom2"></div>
      </div>
      <!-- 网咖的预定 -->
      <div class="primary">
        <!-- 网咖-门店 -->
        <div v-if="istype" class="stores">
          <div class="input">请选择门店</div>
          <div class="stores-icon"></div>
        </div>
        <!-- 网咖-区域座位 -->
        <div v-if="istype" class="stores">
          <div class="input">作为区域类型/座位数</div>
          <div class="stores-icon"></div>
        </div>

        <!-- 酒店-位置 -->
        <div v-if="!istype" class="stores">
          <div class="location-input" @click="goChooseHotel">{{ Area }}</div>
          <div class="location-icon"></div>
        </div>

        <!-- 酒店入住时间 -->
        <div v-if="!istype" class="hotelTime" @click="show = true">
          <div class="hotelTime-left">
            <div class="hotelTime-starIn">
              <div class="hotelTime-starIn-text">入住时间：</div>
              <div class="hotelTime-starIn-text">离店时间：</div>
            </div>
            <div>
              <div class="startTime">
                {{ thisTime }}
              </div>
              <div class="overTime">
                {{ overTime }}
              </div>
            </div>
          </div>
          <div class="hotelTime-right">共{{night}}晚</div>
        </div>
        
        <!-- 选择日期-日期列表 -->
        <van-calendar v-model:show="show" type="range" @confirm="onConfirm" />

        <!-- 预定按钮 -->
        <div v-if="istype" class="getButton">预约订座</div>
        <div v-else class="getButton" @click="goHotelType">查找酒店</div>
        <!-- 网咖功能 -->
        <div v-if="istype" class="intFun">
          <van-swipe :loop="false" :width="70" :show-indicators="false">
            <van-swipe-item v-for="(item, index) in loginForm.icon" :key="index">
              <div class="item-icon">
                <img class="auto-img" :src="item.iconUrl" alt="">
              </div>
              <div class="item-text">{{ item.name }}</div>
            </van-swipe-item>
          </van-swipe>
        </div>
        <!-- 酒店功能 -->
        <div v-else class="intFun">
          <van-swipe :loop="false" :width="70" :show-indicators="false">
            <van-swipe-item v-for="(item, index) in hotelList.icon" :key="index">
              <div class="item-icon">
                <img class="auto-img" :src="item.iconUrl" alt="">
              </div>
              <div class="item-text">{{ item.name }}</div>
            </van-swipe-item>
          </van-swipe>
        </div>
      </div>
    </div>
    <!-- 特惠推荐/品质电竞房 -->
    <div class="specialOffer">
      <div class="specialOffer-top">
        <!-- 特惠推荐 -->
        <div class="federerTeam" v-for="(item, index) in hotelList.themeRecommends" :key="index"
          @click="goFeatures(index)">
          <!-- 标题图片 -->
          <div class="federerTeam-topText">
            <img class="auto-img" :src="item.nameUrl" alt="">
          </div>
          <!-- 房间图片 -->
          <div class="federerTeam-img">
            <img class="auto-img" :src="item.entranceUrl" alt="">
          </div>
        </div>
      </div>
      <div class="specialOffer-bottom"></div>
    </div>
    <!-- 每日签到/任务中心/福利社群 -->
    <div class="welfare">
      <div class="welfare-sigin" v-for="(item, index) in  hotelList.metro" :key="index"
        :style="`background-color: ${item.bgColor}`">
        <div class="welfare-sigin-name">{{ item.name }}</div>
        <div class="welfare-sigin-subtitle">{{ item.subtitle }}</div>
        <div class="welfare-sigin-iconUrl"><img class="auto-img" :src="item.iconUrl" alt=""></div>
      </div>
    </div>
    <!-- 下面列表数据 -->
    <div class="rechargeList">

      <div v-masonry fit-width="false" transition-duration="0.3s" item-selector=".card" origin-left="false"
        class="pets">
        <div class="card">
          <van-swipe :loop="false" :autoplay="1000" :height="210" :width="178">
            <van-swipe-item v-for="(item, index) in hotelList.cardAds" :key="index">
              <img class="auto-img" :src="item.imageUrl" />
            </van-swipe-item>
          </van-swipe>
        </div>
        <div v-masonry-tile v-for="(item, index) in mallProduct.productList" :key="index" class="card">
          <div class="card-img" @click="goDetails(item.id)">
            <img class="auto-img" :src="item.pic" alt="">
          </div>
          <div>
            <p class="card-name1">{{ item.name }}</p>
            <p class="card-name2" v-if="item.promotionPrice">￥{{ item.promotionPrice }}</p>
            <p class="card-name2" v-else>{{ item.currency }}
              <img class="doudou-img" src="@/assets/icon/_P1.png" alt="">
            </p>
            <p class="card-name3">已售{{ getCount(item.sale) }}份</p>
          </div>

        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, onMounted, ref } from 'vue'
// import CommonCard from '../components/CommonCard/index.vue'
import { getFileList, gethotel, getRecommend } from '@/api/api'
import type { HomeFunList, RecommendType, productListType } from '@/tsList/homeFunts'
//路由
import { useRouter, useRoute } from 'vue-router'

const istype = ref(true);

const thisTime = ref<string>('')
const overTime = ref<string>('12月04日  周日')
const night = ref(1)

const date = ref('');
const show = ref(false);

const formatDate = (date: any) => `${date.getMonth() + 1}/${date.getDate()}`;
const onConfirm = (values: any) => {
  const [start, end] = values;
  show.value = false;
  date.value = `${formatDate(start)} - ${formatDate(end)}`;

  let arr1 = formatDate(start).split('/');
  let timeOne = Number(arr1[0]) 
  let timeTwo = Number(arr1[1]) 
  if(timeOne < 10 && timeTwo < 10){
    thisTime.value = '0'+ timeOne + '/' + '0' + timeTwo
  }else if(timeOne < 10 && timeTwo >= 10){
    thisTime.value = '0'+ timeOne + '/' + timeTwo
  }else if (timeOne >= 10 && timeTwo < 10){
    thisTime.value = timeOne + '/' + '0' + timeTwo
  }else {
    thisTime.value = timeOne + '/' + timeTwo
  }

  let arr2 = formatDate(start).split('/');
  let timeFirst = Number(arr2[0]) 
  let timeSecond = Number(arr2[1]) 
  if(timeFirst < 10 && timeSecond < 10){
    overTime.value = '0'+ timeFirst + '/' + '0' + timeSecond
  }else if(timeFirst < 10 && timeSecond >= 10){
    overTime.value = '0'+ timeFirst + '/' + timeSecond
  }else if (timeFirst >= 10 && timeSecond < 10){
    overTime.value = timeFirst + '/' + '0' + timeSecond
  }else {
    overTime.value = timeFirst + '/' + timeSecond
  }

  // if(formatDate(start).charAt(1) == '/'){
  //   thisTime.value = '0' + formatDate(start)
  // }else {
  //   thisTime.value = formatDate(start)
  // }
  // if(formatDate(end).charAt(1) == '/'){
  //   overTime.value = '0' +  formatDate(end)
  // }else {
  //   overTime.value = formatDate(end)
  // }
  console.log('开始时间',formatDate(start),'结束时间',formatDate(end));
  
  let kk = Number((formatDate(start).slice(3, 5)))
  let gg = Number((formatDate(end).slice(3, 5)))
  
  night.value = gg - kk
};

//选择地区
const Area = ref('全国')

let router = useRouter();
let route = useRoute();

const loginForm = reactive<HomeFunList>({
  bannerAds: '',
  cardAds: [],
  focusAds: [],
  icon: [],
  metro: [],
  navigation: [],
  smallIcon: [],
  themeRecommends: []
})

const hotelList = reactive<HomeFunList>({
  bannerAds: '',
  cardAds: [],
  focusAds: [],
  icon: [],
  metro: [],
  navigation: [],
  smallIcon: [],
  themeRecommends: []
})

const mallProduct = reactive<{ productList: Array<productListType> }>({
  productList: []
})

function goHotelType() {
  router.push({
    path: '/HotelTypeList',
    query: {
      thisTime: thisTime.value,
      overTime: overTime.value,
      area: Area.value
    }
  })
}

onMounted(() => {

  //获取当前时间
  var myDate = new Date();
  //月
  let month = myDate.getMonth() + 1
  //日
  let data = myDate.getDate();
  let day = myDate.getDay();

  let nowTime = '';
  if(month< 10 && data < 10) {
    nowTime = '0' + month + '月' + '0' + data + '日' + '   ' + '星期' + day;
  }else if(month< 10){
    nowTime = '0' + month + '月' + data + '日' + '   ' + '星期' + day;
  }else if(data < 10) {
    nowTime = month + '月' + '0' + data + '日' + '   ' + '星期' + day;
  }else {
    nowTime = month + '月' + data + '日' + '   ' + '星期' + day;
  }

  thisTime.value = nowTime;
  
  //选择地区
  if (route.query.area) {
    istype.value = false
    let kk = JSON.parse(JSON.stringify(route.query.area))
    Area.value = kk
  }


  //获取网咖功能列表
  gethotel().then((res) => {

    let kk = JSON.parse(JSON.stringify(res.data))
    hotelList.icon = kk.icon
    hotelList.themeRecommends = kk.themeRecommends
    hotelList.metro = kk.metro
    hotelList.cardAds = kk.cardAds

  })

  //获取酒店功能列表
  getFileList().then((res) => {

    let kk = JSON.parse(JSON.stringify(res.data))
    loginForm.icon = kk.icon
  })

  //获取推荐
  getRecommend().then((res) => {
    let kk = JSON.parse(JSON.stringify(res))
    // console.log('Recommend', kk.data.mallProduct);
    
    mallProduct.productList = kk.data.mallProduct.productList
  })
})

//简写销量
function getCount(count: number) {
  if (count < 10000) {
    return count;
  } else {
    const re = /^\d+\.?\d?/;
    let res = count / 10000 + "";
    let kk = res.match(re)
    if (kk != null) {
      return parseFloat(kk[0]) + "万";
    } else {
      return 0
    }

  }
}

//选择网咖还是酒店
function isType() {
  istype.value = !istype.value
}

//跳转到特色推荐
function goFeatures(index: number) {
  router.push({
    //传递参数使用query的话，指定path或者name都行，但使用params的话，只能使用name指定 
    path: '/FeatureRecommendation',
    query: {
      num: index
    }
  })
}

//跳转到搜索
function goSearch() {
  router.push({
    path: '/SearchView'
  })
}

//跳转到选择页面
function goChooseHotel() {
  router.push({
    path: '/ChooseArea'
  })
}

//跳转到商品详情页面
function goDetails(id:number) {
  router.push({
    path: '/productDetails',
    query:{
      vid: id
    }
  })
}
</script>

<style lang="less" scoped>
.auto-img {
  width: 100%;
  height: 100%;
  display: block;
}

.header {
  width: 100%;
  position: relative;
  padding: 15px 10px 50px;

  .background {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 240px;
    background-image: url('@/assets/icon/7v.png');
    background-size: cover;
    z-index: -1;
  }

  .top {
    width: 100%;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;

    .search {
      flex: 1;
      height: 28px;
      background-color: rgba(221, 221, 221, .3);
      border-radius: 999px;
      padding: 0 10px;
      display: flex;
      align-items: center;

      .search-icon {
        width: 12px;
        height: 12px;
        margin-right: 5px;
        background-size: cover;
        background-image: url('@/assets/icon/Au.png');
      }

      .search-text {
        font-size: 12px;
        color: #e7e7e7;
      }
    }

    .tocode {
      width: 20px;
      height: 20px;
      margin: 0 10px;
      background-image: url('@/assets/icon/5J.png');
      background-size: cover;
    }

  }

  .Internet {
    box-sizing: border-box;
    background-color: white;
    border-radius: 10px;
    border-top-left-radius: 15px;
    border-top-right-radius: 15px;
    margin-top: 20px;

    .Internet-type {
      height: 35px;
      display: flex;
      text-align: center;
      line-height: 35px;
      border-top-left-radius: 15px;
      border-top-right-radius: 15px;
      font-size: 12px;
      color: rgb(97, 97, 97);
      background-image: url('@/assets/icon/Mi.png');
      background-size: 400px 45px;
      background-repeat: no-repeat;
      background-position: 0px -5px;
      position: relative;

      .cafes {
        flex: 1;

        .zindexmax1 {
          width: 100%;
          background-image: url('@/assets/icon/Mi.png');
          background-size: cover;
        }

      }

      .hotel {
        flex: 1;

        .zindexmax2 {
          width: 100%;
          background-image: url('@/assets/icon/Mi.png');
          background-size: cover;
        }
      }

      .active {
        z-index: 11;
        height: 35px;
        font-size: 13px;
        font-weight: 600;
        color: black;
      }

      .tranfrom1 {
        position: absolute;
        z-index: 10;
      }

      .tranfrom2 {
        position: absolute;
        left: 177px;
        z-index: 10;
      }

      .tranfrom3 {
        position: absolute;
        left: 0;
        top: -5px;
        height: 40px;
        width: 50%;
        z-index: 9;
        background-image: url('@/assets/icon/c_.png');
        background-size: cover;
        // transition: myfirst .5s linear;
        animation: myfirst .4s linear;
        animation-fill-mode: forwards
      }

      .tranfrom4 {
        position: absolute;
        left: 177px;
        top: -5px;
        height: 40px;
        width: 50%;
        z-index: 9;
        background-image: url('@/assets/icon/c_.png');
        background-size: cover;
        // transition: myfirst .5s linear;
        animation: myfirst1 .4s linear;
        animation-fill-mode: forwards
      }

      @keyframes myfirst {
        0% {
          left: 0px;
        }

        10% {
          left: 90px;
          transform: rotateY(180deg);
        }

        100% {
          left: 177px;
          transform: rotateY(180deg);
        }
      }

      @keyframes myfirst1 {
        0% {
          left: 177px;
          transform: rotateY(180deg);
        }

        10% {
          left: 90px;
          transform: rotateY(0deg);
        }

        100% {
          left: 0px;
        }
      }
    }
  }

  .primary {
    padding: 0 10px;
    box-sizing: border-box;

    .stores {
      display: flex;
      align-items: center;
      height: 25px;
      border-bottom: 1px solid rgba(221, 221, 221, .3);
      margin: 10px 0;

      .input {
        flex: 1;
        font-size: 13px;
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0 10px;
        color: #808080;
      }

      .stores-icon {
        width: 5px;
        height: 10px;
        background-image: url('@/assets/icon/FO.png');
        background-size: cover;
      }

    }

    .getButton {
      width: 100%;
      height: 35px;
      background-color: #5b6cd4;
      border-radius: 999px;
      color: white;
      font-size: 13px;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .intFun {
      padding: 10px 0;
      font-size: 12px;

      .item-icon {
        width: 30px;
        height: 30px;
      }
    }

    /deep/.van-swipe-item {
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    .location-input {
      color: black;
      flex: 1;
      font-size: 13px;
      font-weight: 600;
      padding: 0 10px;
    }

    .location-icon {
      width: 15px;
      height: 15px;
      background-size: cover;
      background-image: url('@/assets/icon/U71.png');
    }

    .hotelTime {
      display: flex;
      align-items: center;
      padding: 10px 0;
      justify-content: space-between;

      .hotelTime-left {
        display: flex;
        font-size: 13px;
        font-weight: 400;

        .hotelTime-starIn-text {
          font-size: 14px;
          font-weight: 600;
        }
      }

      .hotelTime-right {
        display: flex;
        font-size: 12px;
      }
    }
  }

  .specialOffer {
    margin-top: 10px;
    padding: 0 10px;
    background-color: white;
    border-radius: 6px;
    overflow: hidden;

    .specialOffer-top {
      height: 100px;
      display: flex;


      .federerTeam {
        flex: 1;

        .federerTeam-topText {
          height: 15px;
          width: 80px;
          margin: 10px 0;
        }

        .federerTeam-img {
          width: 135px;
          height: 70px;
          border-radius: 10px;
          margin: 0 auto;
          overflow: hidden;
        }
      }
    }
  }

  .welfare {
    display: flex;
    padding: 10px 10px;
    background-color: white;
    border-radius: 10px;

    .welfare-sigin {
      flex: 1;
      box-sizing: border-box;
      padding: 10px 10px;
      border-radius: 10px;
      position: relative;

      .welfare-sigin-name {
        font-size: 13px;
        font-weight: 600;
        color: #000000;
      }

      .welfare-sigin-subtitle {
        font-size: 12px;
        color: #808080;
        transform: scale(.9);
      }

      .welfare-sigin-iconUrl {
        width: 25px;
        height: 25px;
        position: absolute;
        right: 0;
        bottom: 0;
      }
    }

    .welfare-sigin:nth-child(2) {
      margin: 0 10px;
    }
  }

  .rechargeList {
    .pets {
      width: 100%;

      .card:nth-child(1) {
        width: 50%;
        height: 210px;
        border-radius: 10px;
        overflow: hidden;
      }

      .card {
        width: 48%;
        background-color: white;
        border-radius: 10px;
        margin-bottom: 10px;

        .card-img {
          width: 100%;
          height: 180px;
          border-radius: 10px;

          img {
            border-radius: 10px;
          }
        }

        .card-name1 {
          font-size: 12px;
          font-weight: 500;
          padding: 0 5px;

        }

        .card-name2 {
          font-size: 15px;
          font-weight: 600;
          padding: 0 5px;
          display: flex;
          align-items: center;
        }

        .doudou-img {
          width: 15px;
          height: 15px;
        }

        .card-name3 {
          font-size: 12px;
          color: #808080;
          transform: scale(.8);
          transform-origin: 0 0;
          padding: 0 5px;

        }
      }
    }
  }

}
</style>
